package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity .SysUserSchedule;
import cn.wgx.modules.sys .services.SysUserScheduleService;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "2.2 用户日程(日历)", description = "表:   sys_user_schedule")
@RestController
@RequestMapping(path = "api/sysUserSchedule")
public class SysUserScheduleController extends BaseController{

    @Resource
    SysUserScheduleService service;

    @ApiOperation(value = "列表", notes = "" )
    @ApiPermission(false)
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @ModelAttribute SysUserSchedule record, 
			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
            @ApiParam(value = "是否倒序,默认是")@RequestParam(defaultValue = "true", required = false)Boolean desc
        ) {
        SysUser user = UserUtil.getUser();
        record.setCreateUserId(user.getUser_id());
        return returnData(PageHelper.startPage(page, Math.min(Math.abs(pageSize), maxPageSize)), service.getList(record, desc));
    }

	
    @ApiOperation(value = "获取单个", notes = "" )
    @ApiPermission(false)
	@RequestMapping(path = "get", method = RequestMethod.GET)
    public Map<String, Object> getOne(Integer id) {
		return returnData(service.getOne(id));
    }

	
    @ApiOperation(value = "新建", notes = "必须参数: title" )
    @ApiPermission(false)
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody SysUserSchedule record) {
        if(record == null || StringUtils.isBlank(record.getTitle())){
            return returnErrorMap("参数错误");
        }
        return returnData(service.insertOne(record));
    }

	
    @ApiOperation(value = "修改", notes = "必须参数: schedule_id" )
    @ApiPermission(false)
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    public Map<String, Object> updateOne(@RequestBody SysUserSchedule record) {
        if(record == null || Validation.isNullOrZero(record.getSchedule_id())){
            return returnErrorMap("参数错误");
        }
        if(!isSelf(record.getSchedule_id())){
            return returnErrorMap("非本人日程");
        }

        return returnData(service.updateOne(record));

    }


    @ApiOperation(value = "删除", notes = "必须参数: schedule_id" )
    @ApiPermission(false)
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        if(!isSelf(id)){
            return returnErrorMap("非本人日程");
        }
        return returnData(service.deleteOne(id));
    }

    public boolean isSelf(Integer id){
        if(Validation.isNullOrZero(id)){
            return false;
        }
        SysUserSchedule one = service.getOne(id);
        if(one == null){
            return false;
        }
        SysUser user = UserUtil.getUser();
        if(!one.getCreateUserId().equals(user.getUser_id())){
            return false;
        }
        return true;
    }
	

}